//
//  CalendarViewController.m
//  SportGo
//
//  Created by rimi on 16/9/20.
//  Copyright © 2016年 Yrzteam. All rights reserved.
//
#define WeakObj(o) __weak typeof(o) o##Weak = o;
#define StrongObj(o) __strong typeof(o) o##Strong = o;

#import "CalendarViewController.h"
#import "LJHNewRunViewController1.h"
#import "LJHRecordShowView.h"
#import "LJHMyRecordViewModel.h"
#import "SZCalendarPicker.h"
#import "FBKVOController.h"
@interface CalendarViewController ()

@property (nonatomic, strong, readwrite) LJHRecordShowView *showView;
@property (nonatomic, strong, readwrite) SZCalendarPicker* calendar;
@property (nonatomic, strong, readwrite) LJHMyRecordViewModel *viewModel;


@property (nonatomic, copy, readwrite) NSArray* walkAndRunKcalArray;
@property (nonatomic,copy, readwrite) NSArray* runKcalArray;

@end

@implementation CalendarViewController
- (void)viewWillAppear:(BOOL)animated{
    [self getKcalData];
    [self KVOHandler];

}
- (void)viewDidLoad {
    [super viewDidLoad];
   self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"run"] style:UIBarButtonItemStylePlain target:self action:@selector(nextPage:)];
    self.navigationItem.leftBarButtonItem.tintColor = getColor(48, 206, 248, 1);


    [self initCalendar];
    
    [self initShowView];
    
}
- (void)initCalendar {
    _calendar = [SZCalendarPicker showOnView:self.view];
    _calendar.today = [NSDate date];
    _calendar.date = _calendar.today;
    _calendar.frame = RzCGRectMake(0, 64, 375, 300);
   // _calendar.frame = CGRectMake(0, 64,WIDTH , HEIGHT/2);
    
    WeakObj(self);
    _calendar.changeMonthBlock = ^(NSInteger year,NSInteger month){
        StrongObj(selfWeak)
        [selfWeakStrong.viewModel getWalkAndRunKcalArrayWithMonth:month year:year weigth:65];
        [selfWeakStrong.viewModel getRunKcalArrayWithMonth:month year:year weigth:65];
    };
    
}
- (void)initShowView {
    _showView = [[LJHRecordShowView alloc] initWithFrame:RzCGRectMake(0, 667/2 +30, 375, 667/2 - 64-10)];
    [self.view addSubview:_showView];

}
- (void)getKcalData {
    
    NSDateComponents *comp = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear| NSCalendarUnitMonth| NSCalendarUnitDay) fromDate:[NSDate date]];
    
    [self.viewModel getWalkAndRunKcalArrayWithMonth:comp.month year:comp.year weigth:65];
    
    [self.viewModel getRunKcalArrayWithMonth:comp.month year:comp.year weigth:65];
    
}

- (void)KVOHandler {
    [self.KVOController observe:self.viewModel keyPath:@"updateWalkAndRunData" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld block:^(id observer, id object, NSDictionary *change) {
        if ([self.viewModel.updateWalkAndRunData boolValue]) {
            _walkAndRunKcalArray    = self.viewModel.walkAndRunKcalArray;
            _showView.normalRecords = _walkAndRunKcalArray;
        }
    }];
    
    [self.KVOController observe:self.viewModel keyPath:@"updateRunData" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld block:^(id observer, id object, NSDictionary *change) {
        if ([self.viewModel.updateRunData boolValue]) {
            _runKcalArray                = self.viewModel.runKcalArray;
            _showView.specialRecords    = _runKcalArray;
            _calendar.specialDataArr    = _runKcalArray;
        }
    }];
}


- (void)nextPage:(UIButton *)sender{
    LJHNewRunViewController1 *nrvc = [[LJHNewRunViewController1 alloc] init];
    [self.navigationController pushViewController:nrvc animated:YES];
//    [self.navigationController showViewController:nrvc sender:nil];
}
#pragma mark - getter and setter

- (LJHMyRecordViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[LJHMyRecordViewModel alloc] init];
    }
    return _viewModel;
}





@end
